﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Profari_Heldur_Utan_Um_Prof
{
    public partial class StartChooseProject : Form
    {

        //Code for the connection to the SQL
        #region SQL connection
        private static SqlConnection con = new SqlConnection("Data Source=orninn.com;Initial Catalog=Notendaprofanir;Persist Security Info=True;User ID=SonjaNew;Password=Léttjógúrt");
        private static DataTable dt = new DataTable();
        private static DataSet ds = new DataSet();
        SqlDataReader r = null;
        #endregion
       
        #region Static Variables
        public static int globalgroupsID;
        public static int globalevaluationID;
        public static bool chkIntroEvaluation;
        public static string name;
        public static string ssn;
        public static int? mobile;
        public static string address;
        public static bool? male;
        public static bool? female;
        public static DateTime? birthyear;
        public static string other;
        public static int id;
        #endregion

        #region Variables
        bool chkName;
        bool chkSsn;
        bool chkMobile;  
        bool chkAddress;
        bool chkGender;
        bool chkBirthYear;
        bool chkOther;
        int countHelpPressed = 1;
        #endregion

        public StartChooseProject()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void StartChooseProject_Load(object sender, EventArgs e)
        {
            lblHelpText.Hide();
            fillGroupsCombo();
            if(projectComboBox.Items.Count > 0)
                projectComboBox.SelectedIndex = 0;
        }


        #region Buttons
        private void btnNext_Click(object sender, EventArgs e)
        {
            getCheckBoxInfo();
            if (chkName || chkSsn || chkMobile || chkAddress || chkGender || chkBirthYear || chkOther)
            {
                System.Threading.Thread openInformationBefore = new System.Threading.Thread(new System.Threading.ThreadStart(OpenInformationBefore));
                openInformationBefore.SetApartmentState(System.Threading.ApartmentState.STA);
                openInformationBefore.Start();
                this.Close();
            }
            else
            {
                insertUserInfo();
                System.Threading.Thread introTextTask = new System.Threading.Thread(new System.Threading.ThreadStart(OpenStartTask));
                introTextTask.SetApartmentState(System.Threading.ApartmentState.STA);
                introTextTask.Start();
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            System.Threading.Thread openTesterInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
            openTesterInterface.SetApartmentState(System.Threading.ApartmentState.STA);
            openTesterInterface.Start();
            this.Close();
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            if (countHelpPressed % 2 == 1)
            {
                lblHelpText.Show();
                countHelpPressed++;
            }
            else if (countHelpPressed % 2 == 0)
            {
                lblHelpText.Hide();
                countHelpPressed++;
            }
        }
        #endregion

        #region Events
        private void projectComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            sessionComboBox.Items.Clear();
            getgroupsID();
            fillEvaluationCombo();
            if (sessionComboBox.Items.Count > 0) 
                sessionComboBox.SelectedIndex = 0;
        }

        private void sessionComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            getevaluationID();
        }
        #endregion

        public void setUserInfo()
        {
            getMaxID();
            name = "User " + id;
            ssn = null;
            mobile = 0;
            address = null;
            male = false;
            female = false;
            birthyear = new DateTime(1900, 1, 1);
            other = null;
        }
        //Code for all the SQL commands in this form
        #region SQL Command
        private void fillGroupsCombo()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT name FROM Groups", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    projectComboBox.Items.Add(r["Name"]);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        private void fillEvaluationCombo()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select Evaluation.name from Evaluation inner join GroupEvaluations on Evaluation.id = GroupEvaluations.evaluationID AND GroupEvaluations.groupsID=" + globalgroupsID, con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    sessionComboBox.Items.Add(r["Name"]);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        private void getgroupsID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT id FROM Groups WHERE name ='" + (string)projectComboBox.SelectedItem + "';", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    globalgroupsID = r.GetInt32(0);

                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        private void getevaluationID()
        {
            try
            {
                con.Open(); 
                SqlCommand cmd = new SqlCommand("select Evaluation.ID from Evaluation inner join GroupEvaluations on Evaluation.id = GroupEvaluations.evaluationID AND GroupEvaluations.groupsID='" + globalgroupsID + "' AND Evaluation.name ='" + (string)sessionComboBox.SelectedItem + "';", con);
                r = cmd.ExecuteReader();

                while (r.Read())
                {
                    globalevaluationID= r.GetInt32(0);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
            
        }
        /*
        private bool getCheckboxInfo()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT introBeforeEvaluationID FROM CheckBoxInfo WHERE groupsID=" +globalgroupsID +  " AND evaluationID="+globalevaluationID, con);
                r = cmd.ExecuteReader();

                while (r.Read())
                {
                    return r.GetBoolean(0);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }

            return false;
        }*/

        public void getCheckBoxInfo()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select C.introBeforeEvaluationID, C.name, C.ssn, C.mobile, C.addres, C.gender, C.BirthYear, C.other from checkBoxInfo C where groupsID=" + globalgroupsID + " and evaluationID=" + globalevaluationID, con);

                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    chkIntroEvaluation = r.GetBoolean(0);
                    chkName = r.GetBoolean(1);
                    chkSsn = r.GetBoolean(2);
                    chkMobile = r.GetBoolean(3);
                    chkAddress = r.GetBoolean(4);
                    chkGender = r.GetBoolean(5);
                    chkBirthYear = r.GetBoolean(6);
                    chkOther = r.GetBoolean(7);

                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());

            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        private void getMaxID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT TOP 1 id FROM UserInfo ORDER BY id desc", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    id = r.GetInt32(0);
                    id++;
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        private void insertUserInfo()
        {
            //setUserInfo();
            getMaxID();
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO UserInfo(name ,ssn ,mobile ,usersAddress ,female ,male ,birthD ,other ) VALUES(@name, @ssn, @mobile, @userAddress, @female, @male, @birthD, @other)", con);
                cmd.Parameters.AddWithValue("@name", "User " + id );
                cmd.Parameters.AddWithValue("@ssn", DBNull.Value );
                cmd.Parameters.AddWithValue("@mobile", DBNull.Value);
                cmd.Parameters.AddWithValue("@userAddress",DBNull.Value);
                cmd.Parameters.AddWithValue("@female", DBNull.Value);
                cmd.Parameters.AddWithValue("@male", DBNull.Value);
                cmd.Parameters.AddWithValue("@birthD", DBNull.Value);
                cmd.Parameters.AddWithValue("@other", DBNull.Value);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }

        #endregion

        //The Open forms functions makes it able to move between forms and close the first form when the second one is opened
        #region OpenForm
        public static void OpenInformationBefore()
        {
            Application.Run(new InformationBefore());
        }
        public static void OpenTesterInterface()
        {
            Application.Run(new TesterInterface());
        }
        public void OpenStartTask()
        {
            Application.Run(new StartTask());
        }

        #endregion
     
    }
}
